package me.raih.controller.user;

import me.raih.pojo.po.Category;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.CategoryService;
import me.raih.service.WaresService;
import me.raih.util.CaseBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Raih
 * @Time 2021/1/25  13:53
 *
 * 关于 头上菜单的功能控制
 *
 */
@Controller
public class UserCategoryController {

    @Autowired
    ResultInfo resultInfo;

    @Autowired
    CategoryService categoryService;

    @Autowired
    WaresService waresService;

    @RequestMapping(value = "/user/findCategory",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo toFindCategory(HttpSession session){
        List<Category> categoryList;
        //获取是否已经在session中保存
        Object category_list = session.getAttribute("CATEGORY_LIST");
        if(category_list == null){
            categoryList = categoryService.findCategory();
            session.setAttribute("CATEGORY_LIST",categoryList);
        }else {
            //如果在session中就不要查了，直接取出来就OK
            categoryList = CaseBeanUtil.castBean(category_list,Category.class);
        }
        resultInfo.setData(categoryList);
        resultInfo.setFlag(true);

        return resultInfo;
    }


    /**
     * 通过传来的category no 可以直接到达某页面
     * @param no
     * @return
     */
    @RequestMapping(value = "/user/category")
    public String toCategoryPage(@RequestParam("no")int no, Model model){

        Category category = categoryService.findCategoryByNo(no);
        //传到页面上去一个当前的 CATEGORY_NO
        model.addAttribute("CATEGORY_NO",no);
        return "user/category/"+category.getUrl();
    }


    @RequestMapping(value = "/user/category/findWaresMessage")
    @ResponseBody
    public ResultInfo findWaresMessage(@RequestParam("currentPage")int currentPage,
                                       @RequestParam("categoryNo")int categoryNo,
                                       @RequestParam("pageSize")int pageSize
                                       ){
        //先固定好10页吧
        pageSize = 10;
        PageBean<Wares> pageBean = waresService.findWaresByCategoryNo(categoryNo, currentPage, pageSize);

        resultInfo.setFlag(true);
        resultInfo.setData(pageBean);

        return resultInfo;
    }


    @RequestMapping(value = "/user/category/searchWares",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo searchWares(@RequestParam("currentPage") int currentPage,@RequestParam("waresName") String waresName){

        PageBean<Wares> pageBean = waresService.findWaresByWaresName(waresName,currentPage,10);
        resultInfo.setFlag(true);
        resultInfo.setData(pageBean);

        return resultInfo;
    }


    @RequestMapping(value = "/user/category/search")
    public String toSearchPage(@RequestParam("waresName") String waresName,Model model){
        PageBean<Wares> pageBean = waresService.findWaresByWaresName(waresName,1,10);

        List<Wares> list = pageBean.getList();
        //将查询的商品信息保存在 list 中
        model.addAttribute("SEARCH_WARES_LIST",list);

        return "user/category/search";
    }

}
